package com.ruoyi.system.service.impl;


import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.BusiException;
import com.ruoyi.common.utils.BigDecimalUtil;
import com.ruoyi.system.biz.CashBizService;
import com.ruoyi.system.domain.Cash;
import com.ruoyi.system.domain.CashHistory;
import com.ruoyi.system.domain.SysConfig;
import com.ruoyi.system.domain.Withdraw;
import com.ruoyi.system.mapper.WithdrawMapper;
import com.ruoyi.system.service.BaseServiceImpl;
import com.ruoyi.system.service.CashHistoryService;
import com.ruoyi.system.service.ISysConfigService;
import com.ruoyi.system.service.WithdrawService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;


@Service
public class WithdrawServiceImpl extends BaseServiceImpl<WithdrawMapper, Withdraw> implements WithdrawService {

    @Autowired
    private CashBizService cashService;
    @Autowired
    private CashHistoryService cashHistoryService;


    //Long userId, BigDecimal money, String remark, String orderNo)
    //用户提现
    @Override
    @Transactional
    public void submitWithdrawal(SysUser user, BigDecimal amount, String address) {
        BigDecimal serviceFee = amount.multiply(new BigDecimal(0.02));

        submitWithdrawalExcute(user, amount, address, serviceFee, "用户提现");
    }

    @Transactional
    public void submitWithdrawalExcute(SysUser user, BigDecimal amount, String address, BigDecimal serviceFee, String remark) {
        Long userId = user.getUserId();
        Cash cash = cashService.getByUserId(userId);
        if (amount.compareTo(BigDecimal.ZERO) <= 0) {
            throw new BusiException("请输入正数");
        }
        if (amount.compareTo(cash.getEarning()) > 0) {
            throw new BusiException("金额不足");
        }
        Withdraw withdraw = new Withdraw();
        withdraw.setUserId(userId);
        withdraw.setMoney(amount);
        withdraw.setAddress(address);
        withdraw.setStatus("reviewed");
        withdraw.setType("withdraw");

        withdraw.setUsername(user.getUserName());
        withdraw.setServiceFee(serviceFee);
        withdraw.setActualAmount(withdraw.getMoney().add(withdraw.getServiceFee()));
        withdraw.setRemark(remark);
        this.addDomain(withdraw);
        CashHistory userCashHis = new CashHistory(userId, withdraw.getActualAmount(), "reduce", remark, "withdraw");
        userCashHis.setOrderNo(withdraw.getId() + "");
        cashService.updateCash(userId, userCashHis);

    }

    @Override
    @Transactional
    public void agentSubmitWithdrawal(SysUser user, BigDecimal amount, String address) {
        submitWithdrawalExcute(user, amount, address, BigDecimal.ZERO, "商户提现");

    }

    @Autowired
    private ISysConfigService iSysConfigService;

    @Override
    @Transactional
    public Long submitRecharge(SysUser user, BigDecimal amount, String address) {
        Long userId = user.getUserId();
        SysConfig sysConfig = new SysConfig();
        sysConfig.setConfigKey("cashAddress");
        if (amount.compareTo(BigDecimal.ZERO) <= 0) {
            throw new BusiException("请输入正数");
        }
        //平台地址
        sysConfig.setConfigKey("cashAddress");
        sysConfig = iSysConfigService.getDomain(sysConfig);
        //充值费率
        SysConfig usdtRate = iSysConfigService.getUsdtRate();


        String configValue = sysConfig.getConfigValue();
        Withdraw withdraw = new Withdraw();
        withdraw.setUsdtRate(new BigDecimal(usdtRate.getConfigValue()));
        withdraw.setCashAddress(configValue);
        withdraw.setUserId(userId);
        withdraw.setMoney(amount);
        withdraw.setAddress(address);
        withdraw.setStatus("init");
        withdraw.setType("recharge");
        withdraw.setUsername(user.getUserName());
        withdraw.setRemark("用户充值");
        withdraw.setServiceFee(new BigDecimal(0));
        withdraw.setActualAmount(withdraw.getMoney().add(withdraw.getServiceFee()));

        this.addDomain(withdraw);
        return withdraw.getId();

    }

    //通过提现申请
    @Override
    @Transactional
    public void pass(Long withdrawId) {
        Withdraw withdraw = this.check(withdrawId);
        if (withdraw == null) {
            throw new BusiException("数据不存在");
        }
        if (!"reviewed".equals(withdraw.getStatus())) {
            throw new BusiException("状态有误");
        }
        withdraw.setStatus("pass");
        this.updateDomain(withdraw);
        if (StrUtil.equals(withdraw.getType(), "recharge")) {
            BigDecimal usdtRate = withdraw.getUsdtRate();
            BigDecimal multiply = withdraw.getMoney().multiply(usdtRate);
            CashHistory userCashHis = new CashHistory(withdraw.getUserId(), BigDecimalUtil.setDecimal(multiply), "add", "充值到账", "recharge");
            userCashHis.setOrderNo(withdraw.getId() + "");
            cashService.updateCash(withdraw.getUserId(), userCashHis);
        }
    }
    @Override
    //拒绝提现申请
    @Transactional
    public void cancel(Long withdrawId) {
        Withdraw withdraw = this.check(withdrawId);
        if (!"reviewed".equals(withdraw.getStatus())) {
            throw new BusiException("状态有误");
        }
        withdraw.setStatus("cancel");
        this.updateDomain(withdraw);
        Long userId = withdraw.getUserId();
        if (StrUtil.equals(withdraw.getType(), "withdraw")) {
            CashHistory userCashHis = new CashHistory(userId, withdraw.getActualAmount(), "add", "提现驳回", "withdrawReject");
            userCashHis.setOrderNo(withdraw.getId() + "");
            cashService.updateCash(userId, userCashHis);
        }

    }


}
